package com.sx.zongzhi.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.sx.zongzhi.R;
import com.sx.zongzhi.activity.AppealActivity;
import com.sx.zongzhi.activity.DisputePendingActivity;
import com.sx.zongzhi.bean.MainPagerBean;
import com.sx.zongzhi.bean.MsgEvent;
import com.sx.zongzhi.bean.OrganizingBean;
import com.sx.zongzhi.bean.UserBean;
import com.sx.zongzhi.presenter.MainPagerPest;
import com.sx.zongzhi.utils.MyRxBus;
import com.sx.zongzhi.utils.ToastUtils;
import com.sx.zongzhi.view.IMainPagerView;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import xiaofei.library.datastorage.DataStorageFactory;
import xiaofei.library.datastorage.IDataStorage;

/**
 * Created by XZY on 2017/11/21.
 * 首页
 */
public class MainPagerFragment extends LazyFragment implements IMainPagerView{
    //当前区域
    @BindView(R.id.tv_CityTit)
    TextView tv_CityTit;
    //户籍人口
    @BindView(R.id.tv_Residence)
    TextView tv_Residence;
    //流动人口
    @BindView(R.id.tv_Move)
    TextView tv_Move;
    //常住人口
    @BindView(R.id.tv_Fixed)
    TextView tv_Fixed;
    //实有房屋
    @BindView(R.id.tv_Houses)
    TextView tv_Houses;
    //出租房
    @BindView(R.id.tv_Rental)
    TextView tv_Rental;
    //矛盾纠纷待办
    @BindView(R.id.tv1_Pending)
    TextView tv1_Pending;
    //矛盾纠纷已办
    @BindView(R.id.tv1_Handled)
    TextView tv1_Handled;
    //矛盾纠纷已办结
    @BindView(R.id.tv1_Done)
    TextView tv1_Done;
    //诉讼服务待办
    @BindView(R.id.tv2_Pending)
    TextView tv2_Pending;
    //诉讼服务已办
    @BindView(R.id.tv2_Handled)
    TextView tv2_Handled;
    //诉讼服务已办结
    @BindView(R.id.tv2_Done)
    TextView tv2_Done;
    private boolean isPrepared=false;
    private MainPagerPest mainPagerPest;
    private String orgCode="";
    private Disposable disposable;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_main_pager,container,false);
        ButterKnife.bind(this,view);
        initView();
        isPrepared=true;
        lazyLoad();
        return view;
    }

    private void initView() {
        IDataStorage dataStorage = DataStorageFactory.getInstance(
                getActivity().getApplicationContext(), DataStorageFactory.TYPE_DATABASE);
        if (dataStorage.contains(UserBean.class,getActivity().getPackageName())){
            UserBean userBean=dataStorage.load(UserBean.class,getActivity().getPackageName());
            tv_CityTit.setText(userBean.getOrg().getOrgName());
        }
        //订阅筛选组织机构
        disposable=MyRxBus.get().toObservable(MsgEvent.class).observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<MsgEvent>() {
                    @Override
                    public void accept(MsgEvent bean) throws Exception {
                            if ("MainPagerFragment".equals(bean.getRequest())) {
                                OrganizingBean oBean= (OrganizingBean) bean.getMsg();
                                if (oBean!=null) {
                                    orgCode = oBean.getOrgCode();
                                    tv_CityTit.setText(oBean.getName());
                                    initData();
                                }
                            }

                    }
                });
    }

    /**
     * 初始化数据
     */
    public void initData() {
        mainPagerPest=new MainPagerPest(this);
        mainPagerPest.getMainPager(orgCode);
    }

    @OnClick({R.id.ll1_Pending,R.id.ll1_Handled,R.id.ll1_Done,R.id.ll2_Pending,
            R.id.ll2_Handled,R.id.ll2_Done})
    public void OnClick(View view){
        switch (view.getId()){
            case R.id.ll1_Pending:
                startActivity(new Intent(getActivity(), DisputePendingActivity.class)
                        .putExtra("DEAL_TYPE","矛盾纠纷-待办"));
                break;
            case R.id.ll1_Handled:
                startActivity(new Intent(getActivity(), DisputePendingActivity.class)
                        .putExtra("DEAL_TYPE","矛盾纠纷-已办"));
                break;
            case R.id.ll1_Done:
                startActivity(new Intent(getActivity(), DisputePendingActivity.class)
                        .putExtra("DEAL_TYPE","矛盾纠纷-办结"));
                break;
            case R.id.ll2_Pending:
                startActivity(new Intent(getActivity(), AppealActivity.class)
                        .putExtra("DEAL_TYPE",0));
                break;
            case R.id.ll2_Handled:
                startActivity(new Intent(getActivity(), AppealActivity.class)
                        .putExtra("DEAL_TYPE",1));
                break;
            case R.id.ll2_Done:
                startActivity(new Intent(getActivity(), AppealActivity.class)
                        .putExtra("DEAL_TYPE",2));
                break;
        }
    }


    @Override
    protected void lazyLoad() {
        if(!isPrepared || !isVisible) {
            return;
        }
        initData();
    }

    @Override
    public void onDestroyView() {
        if (disposable!=null){
            disposable.dispose();
        }
        isPrepared=false;
        super.onDestroyView();
    }

    @Override
    public void getMainPager(MainPagerBean pagerBean) {
        setViews(pagerBean);
    }
    public void setViews(MainPagerBean pagerBean) {
        tv_Residence.setText(String.valueOf(pagerBean.getHouseholdPopulation()));
        tv_Move.setText(String.valueOf(pagerBean.getFloatingPopulation()));
        tv_Fixed.setText(String.valueOf(pagerBean.getResidentPopulation()));
        tv_Houses.setText(String.valueOf(pagerBean.getActualHouse()));
        tv_Rental.setText(String.valueOf(pagerBean.getRentalHouse()));
        tv1_Pending.setText(String.valueOf(pagerBean.getIssues_needDo()));
        tv1_Handled.setText(String.valueOf(pagerBean.getIssues_done()));
        tv1_Done.setText(String.valueOf(pagerBean.getIssues_completed()));
        tv2_Pending.setText(String.valueOf(pagerBean.getAppeal_needDo()));
        tv2_Handled.setText(String.valueOf(pagerBean.getAppeal_done()));
        tv2_Done.setText(String.valueOf(pagerBean.getAppeal_completed()));
    }

    @Override
    public void showError(String message) {
        ToastUtils.showToast(getActivity(),message);
    }

}
